﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Core;
using System.Reflection;
using System.Runtime.Serialization;
using System.Linq.Expressions;

namespace Bzure.Common.Data
{
    /// <summary>
    /// 实体提交
    /// </summary>
    public class EntityPostRequest : EntityRequest
    {
        public enum EntityPostMethod
        {
            Create = 1,//创建实体
            Update = 2,//更新实体
            Delete = 3//删除实体
        }
        public EntityPostRequest(EntityPostMethod method)
        {
            this._mothod = method;
        }
        /// <summary>
        /// 实体
        /// </summary>
        public object Entity { get; set; }
        private EntityPostMethod _mothod;
        //实体操作方法
        public EntityPostMethod Method { get { return _mothod; } }
        List<EntityMemberMap> _FieldMap = new List<EntityMemberMap>();
        /// <summary>
        /// 字段映射
        /// </summary>
        public EntityMemberMap[] FieldMap
        {
            get
            {
                return _FieldMap.ToArray();
            }
        }
        List<IEntityMemberMap> _ConditionMap = new List<IEntityMemberMap>();
        /// <summary>
        /// 条件映射
        /// </summary>
        public IEntityMemberMap[] ConditionMap
        {
            get
            {
                return _ConditionMap.ToArray();
            }
        }


        public EntityPostRequest AppendFieldMap<T>(object MemberValue, Expression<Func<T>> e)
        {
            this._FieldMap.Add(MemberHelper.MemberMap(MemberValue, e));
            return this;
        }
        public EntityPostRequest AppendConditionMap<T>(object MemberValue, Expression<Func<T>> e, EntityRequest.Operator Operator = EntityRequest.Operator.Equal, EntityRequest.LogicSymbol LogicSymbol = EntityRequest.LogicSymbol.And)
        {
            this._ConditionMap.Add(MemberHelper.MemberMap(MemberValue, e, Operator, LogicSymbol));
            return this;
        }
        public EntityPostRequest AppendLogicLink(EntityRequest.LogicLink LogicLink, EntityRequest.LogicSymbol LogicSymbol = LogicSymbol.And)
        {
            this._ConditionMap.Add(new LogicLinkMap() { LogicLink = LogicLink, LogicSymbol = LogicSymbol });
            return this;
        }
    }
    /// <summary>
    /// 实体查询
    /// </summary>
    public class EntityQueryRequest<T> : EntityRequest
    {
        List<IEntityMemberMap> _ConditionMap = new List<IEntityMemberMap>();
        /// <summary>
        /// 条件映射
        /// </summary>
        public IEntityMemberMap[] ConditionMap
        {
            get
            {
                return _ConditionMap.ToArray();
            }
        }
        public EntityQueryRequest<T> AppendConditionMap<Member>(object MemberValue, Expression<Func<Member>> e, EntityRequest.Operator Operator = EntityRequest.Operator.Equal, EntityRequest.LogicSymbol LogicSymbol = EntityRequest.LogicSymbol.And)
        {
            this._ConditionMap.Add(MemberHelper.MemberMap(MemberValue, e, Operator, LogicSymbol));
            return this;
        }
        public EntityQueryRequest<T> AppendLogicLink(EntityRequest.LogicLink LogicLink, EntityRequest.LogicSymbol LogicSymbol = LogicSymbol.And)
        {
            this._ConditionMap.Add(new LogicLinkMap() { LogicLink = LogicLink, LogicSymbol = LogicSymbol });
            return this;
        }
    }
    /// <summary>
    /// 实体分页查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class EntityPageQueryRequest<T> : EntityQueryRequest<T>
    {
        public class OrderMap
        {
            public EntityMemberMap OrderByMember { get; set; }
            public OrderByType OrderType { get; set; }
        }
        public EntityPageQueryRequest()
        {
            this.OrderType = OrderByType.DESC;
            this.MinorOrders = new List<OrderMap>();
        }
        public enum OrderByType
        {
            DESC = 1,//降序
            ASC = 2//升序
        }
        public List<OrderMap> MinorOrders { get; set; }
        /// <summary>
        /// 排序成员
        /// </summary>
        public EntityMemberMap OrderByMember { get; private set; }
        /// <summary>
        /// 排序类型
        /// </summary>
        public OrderByType OrderType { get; set; }
        /// <summary>
        /// 页尺寸
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage { get; set; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public long TotalRecord { get; set; }
        public string OrderTypeString(OrderByType orderType)
        {
            switch (orderType)
            {
                case OrderByType.ASC:
                    return "ASC";
                default:
                    return "DESC";
            }

        }
        public string OrderTypeString()
        {
            return OrderTypeString(this.OrderType);
        }
        public EntityQueryRequest<T> SetOrderByMember<Member>(object MemberValue, Expression<Func<Member>> e, EntityRequest.Operator Operator = EntityRequest.Operator.Equal)
        {
            this.OrderByMember = MemberHelper.MemberMap(MemberValue, e, Operator);
            return this;
        }
        public EntityQueryRequest<T> AppendMinorOrderByMember<Member>(object MemberValue, Expression<Func<Member>> e, OrderByType OrderByType, EntityRequest.Operator Operator = EntityRequest.Operator.Equal)
        {
            this.MinorOrders.Add(new OrderMap()
            {
                OrderByMember =MemberHelper.MemberMap(MemberValue, e, Operator),
                OrderType = OrderType
            });
            return this;
        }
    }
}
